package com.itheima.zuoye.zuoye2;

import com.itheima.zuoye.zuoye1.Acc;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Money implements Runnable {
    int[] money = {10, 50, 20, 50, 100, 200, 500, 800, 2, 80, 300, 700};
    List<Integer> list;
    int max = 0;
    int he = 0;

    //记录剩余的金额
    ArrayList<Integer> sum1 = new ArrayList<>();
    ArrayList<Integer> sum2 = new ArrayList<>();


    public Money() {
        list = new ArrayList<>();
        for (int i : money) {
            list.add(i);
        }
    }


    @Override
    public void run() {

        while (true) {
            boolean money1 = getMoney();
            if (!money1) {
                break;
            }

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


        }
    }

    private synchronized boolean getMoney() {
        Random r = new Random();
        if (list.isEmpty()) {
            System.out.println("所有奖金已经全部抽完");
            return false;
        }
        int index = r.nextInt(list.size());
        Integer sum = list.remove(index);
        if (Thread.currentThread().getName().equals("抽奖箱1")) {
            sum1.add(sum);
        } else {
            sum2.add(sum);
        }


        // System.out.println(Thread.currentThread().getName()+"最大值为：" + max+"，和为：" + he);

        System.out.println(Thread.currentThread().getName() + "又产生了" + sum + "元奖金");

        return true;


    }

}
